﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFServices.Models;
using WPFServices.Models.Domain;
using WPFServices.Services;

namespace WPFServices.Services.DomainService.ProductPlaninfo
{
    public class ProductPlanService
    {
        private readonly ApiClient _apiClient;

        public ProductPlanService()
        {
            _apiClient = new ApiClient();
        }

        /// <summary>
        /// 获取所有生产计划
        /// </summary>
        public async Task<ApiPaging<List<ProductPlanDto>>> SearchProductPlanAsync(SearchProductPlanDto searchProductPlanDto)
        {
            try
            {
                var queryParams = $"PageIndex={searchProductPlanDto.PageIndex}&PageSize={searchProductPlanDto.PageSize}";
                if (!string.IsNullOrEmpty(searchProductPlanDto.Plan_Id))
                {
                    queryParams += $"&Plan_Id={Uri.EscapeDataString(searchProductPlanDto.Plan_Id)}";
                }
                if (!string.IsNullOrEmpty(searchProductPlanDto.Plan_Name))
                {
                    queryParams += $"&Plan_Name={Uri.EscapeDataString(searchProductPlanDto.Plan_Name)}";
                }
                // 为FromType设置默认值0
                var fromType = searchProductPlanDto.FromType ?? 0;
                queryParams += $"&FromType={fromType}";
                
                if (!string.IsNullOrEmpty(searchProductPlanDto.ProductName))
                {
                    queryParams += $"&ProductName={Uri.EscapeDataString(searchProductPlanDto.ProductName)}";
                }
                // 为Status设置默认值0
                var status = searchProductPlanDto.Status ?? 0;
                queryParams += $"&Status={status}";

                var url = $"Plan/GetIProductionPlanServiceById?{queryParams}";
                System.Diagnostics.Debug.WriteLine($"调用API: {url}");
                
                var result = await _apiClient.GetAsync<ApiPaging<List<ProductPlanDto>>>(url);
                System.Diagnostics.Debug.WriteLine($"API响应: IsSuc={result.IsSuc}, Code={result.code}, Msg={result.msg}");
                
                if (result.IsSuc && result.code == ResultCode.Ok)
                {
                    System.Diagnostics.Debug.WriteLine($"数据加载成功: TotalCount={result.data?.TotalCount}, TotalPage={result.data?.TotalPage}");
                    return result.data;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine($"API调用失败: {result.msg}");
                    throw new Exception($"获取生产计划列表失败: {result.msg}");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"API调用异常: {ex.Message}");
                throw new Exception($"获取生产计划列表失败: {ex.Message}");
            }
        }



        /// <summary>
        /// 分解生产计划
        /// </summary>
        public async Task<ApiResult> DismantleProductPlanAsync(int id)
        {
            try
            {
                var url = $"Plan/ProductionDismantle?id={id}";
                System.Diagnostics.Debug.WriteLine($"调用分解API: {url}");
                
                var result = await _apiClient.PostAsync<ApiResult>(url);
                System.Diagnostics.Debug.WriteLine($"分解API响应: IsSuc={result.IsSuc}, Code={result.code}, Msg={result.msg}");
                
                // 如果API返回失败，提供更详细的错误信息
                if (!result.IsSuc)
                {
                    System.Diagnostics.Debug.WriteLine($"分解API返回失败: {result.msg}");
                    return new ApiResult(false, ResultCode.Fail, $"后端分解失败: {result.msg}");
                }
                
                return result;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"分解API调用异常: {ex.Message}");
                return new ApiResult(false, ResultCode.Fail, $"分解生产计划失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 新增生产计划
        /// </summary>
        public async Task<ProductPlanDto> AddProductPlanAsync(CreateUpdateProductionPlanDto createDto)
        {
            try
            {
                var url = "Plan/AddIProductionPlanService";
                var result = await _apiClient.PostAsync<ProductPlanDto>(url, createDto);
                
                if (result.IsSuc && result.code == ResultCode.Ok)
                {
                    return result.data;
                }
                else
                {
                    throw new Exception($"新增生产计划失败: {result.msg}");
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"新增生产计划失败: {ex.Message}");
            }
        }
    }
}
